package com.niit.agricstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.niit.agricstore.entity.Notice;
import com.niit.agricstore.mapper.NoticeMapper;
import com.niit.agricstore.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper,Notice> implements NoticeService {
    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public int createNotice(Notice notice) {
        return noticeMapper.insert(notice);
    }

    @Override
    public int updateNotice(int noticeId, Notice newNotice) {
        return noticeMapper.updateById(newNotice);
    }

    @Override
    public int deleteNotice(int noticeId) {
        Notice notice = noticeMapper.selectById(noticeId);
        return noticeMapper.deleteById(notice);
    }

    @Override
    public int deleteNoticeAll() {
        return noticeMapper.delete(null);
    }

    @Override
    public List<Notice> getAllNotices() {
        return noticeMapper.selectList(null);
    }

    @Override
    public Notice getNoticeById(int noticeId) {
        return noticeMapper.selectById(noticeId);
    }

    @Override
    public List<Notice> getNoticesByType(int noticeType) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", noticeType);
        return noticeMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Notice> getNoticesByPage(int page, int pageSize) {
        Page<Notice> noticePage = new Page<>(page, pageSize);
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.orderByDesc("notice_create_time");
        return noticeMapper.selectPage(noticePage, queryWrapper);
    }

    @Override
    public void updateNoticeStatus(int noticeId, int status) {
//        Notice notice = noticeMapper.selectById(noticeId);
//        if (notice != null) {
//            notice.setStatus(status);
//            notice.setUpdateTime(new Date());
//            noticeMapper.updateById(notice);
//        }
    }

    @Override
    public PageInfo<Notice> findNoticeAllByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("notice_create_time");
        List<Notice> noticeList = noticeMapper.selectList(queryWrapper); // 或者您自定义的查询方法
        return new PageInfo<>(noticeList);
    }

    //根据用户ID选择信息
    @Override
    public PageInfo<Notice> getNoticesByUid(int noticeWho, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("notice_who",noticeWho);
        List<Notice> noticeList = noticeMapper.selectList(queryWrapper);
        return new PageInfo<>(noticeList);
    }
}
